﻿using System;
using System.Text;
using RabbitMQ.Client;
using System.Threading;
using RabbitMQ.Shared;

namespace RabbitMQProj
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //1、创建连接工厂
            var factory = new ConnectionFactory();
            factory.HostName = "localhost";

            //1.1、通过工厂创建连接
            var conn = factory.CreateConnection();

            //2、通过连接创建通道 Channel
            using (var channel = conn.CreateModel())
            {
                //3、通过通道声明队列
                //4、持久化----队列
                //channel.QueueDeclare(queue: "lhy", durable: true, exclusive: false, autoDelete: false, arguments: null);

                //声明交换器
                channel.ExchangeDeclare(Exchange.Name, ExchangeType.Topic);

                //消息数据                
                bool stop = false;
                
                while (!stop)
                {
                    string messageBody = Console.ReadLine();

                    //消息的字节数组
                    //4、准备发送消息的二进制数据
                    byte[] bytes = Encoding.UTF8.GetBytes(messageBody);

                    var properties = channel.CreateBasicProperties();

                    properties.Persistent = true;

                    //5、调用BasicPublish发布消息
                    channel.BasicPublish(exchange: Exchange.Name, routingKey: args[0], basicProperties: null, body: bytes);                    

                    //消息发布完成
                    Console.WriteLine("消息发布完成...");                    

                    if (messageBody == "stop")
                    {
                        stop = true;
                    }
                }

                /*
                for (int i = 0; i < 15; i++)
                {
                    string messageBody = $"第{i}条消息";

                    //消息的字节数组
                    //4、准备发送消息的二进制数据
                    byte[] bytes = Encoding.UTF8.GetBytes(messageBody);

                    var properties = channel.CreateBasicProperties();

                    properties.Persistent = true;

                    //5、调用BasicPublish发布消息
                    channel.BasicPublish(exchange: "", routingKey: "lhy", basicProperties: properties, body: bytes);

                    //消息发布完成
                    Console.WriteLine("消息发布完成...");
                }
                */
            }

            Console.ReadLine();
        }
    }
}
